//Api集中管理,使用vuex存储token
//而具体的接口函数则应该封装在 api.js 文件中。
const http = uni.$u.http
import store from '@/store/index.js'
// export const wxLogin = (params)=>http.put('/user/wxlogin', params)







// //登录
export async function wxlogin(params) {
	try {
		const res = await http.put('/user/wxlogin', params);

		uni.$u.http.setConfig((config) => {
			config.header.wxtoken = res.wxtoken
			return config
		})
		uni.setStorageSync('wxtoken', res.wxtoken)

		return res;
	} catch (error) {
		console.log('request fail wxlogin()', error);
		throw error; // 抛出错误
	}
}



//用户的 发布的 谜题的获取
export async function showUserList(id) {
	try {
		const res = await http.get(`/user/puzzle/list`)


		return res;
	} catch (error) {
		console.log('showUserList() request fail d', error);
		throw error; // 抛出错误
	}
}

// 谜题的单个新增
export async function addPuzzle(formdata) {
	try {
		// const res = await  http.get(`/user/getuserinfo`,{
		const res = await http.post(`/user/puzzle/`, formdata)

		return res;

	} catch (error) {
		console.log('addPuzzle() request fail addPuzzle', error);
		throw error; // 抛出错误
	}
}
// 谜题的单个删除
export async function deletePuzzle(id) {
	try {
		// const res = await  http.get(`/user/getuserinfo`,{
		const res = await http.delete(`/user/puzzle/`, id)

		return res;

	} catch (error) {
		console.log('deletePuzzle() request fail deletePuzzle', error);
		throw error; // 抛出错误
	}
}
//获取用户  更多信息
export async function getUserInfo() {
	try {
		const res = await http.get(`/user/getuserinfo`)

		return res;

	} catch (error) {
		console.log('addPuzzle() request fail addPuzzle', error);
		throw error; // 抛出错误
	}
}

export async function getGameHistory() {
	return await http.get('/user/gamehistory/')
}

//更新 用户  信息
export async function setUserInfo(formdata) {
	try {
		const res = await http.post(`/user/update`, formdata)

		return res;

	} catch (error) {
		console.log('setUserInfo() request fail ', error);
		throw error; // 抛出错误
	}
}


// 朋友id和留言   添加朋友
export async function addFriend(formdata) {
	try {

		const res = await http.post(`/user/partner/add`, formdata)

		return res;

	} catch (error) {
		console.log('addFriend() request fail ', error);
		throw error; // 抛出错误
	}
}
// 通过添加状态查询朋友列表
export async function partnerList(status) {
	try {
		const res = await http.get(`/user/partner/list`, {

			//0(待同意列表)，1（已添加）
			params: {
				status: status
			},
		})

		return res;

	} catch (error) {
		console.log('partnerList() request fail ', error);
		throw error; // 抛出错误
	}
}

// 通过朋友id获取聊天信息
export async function chatSingleList(id) {
	//当前用户 与谁聊天，id就是谁
	try {
		const res = await http.get(`/user/chat/list`, {

			//0(待同意列表)，1（已添加）
			params: {
				id: id
			},
		})

		return res;

	} catch (error) {
		console.log('chatSingleList() request fail ', error);
		throw error; // 抛出错误
	}
}

// 根据用户姓名或手机号查找用户
export async function getPersonById(findInfo) {
	try {
		const res = await http.get(`/user/partner/`, {

			//0(待同意列表)，1（已添加）
			params: {
				findInfo: findInfo
			},
		})

		return res;

	} catch (error) {
		console.log('getPersonById() request fail ', error);
		throw error; // 抛出错误
	}
}

// 上传图片
export async function commonUpload(data) {
	//data => count:int  、files：array

	try {
		const res = await http.post(`/common/update`, data)

		return res;

	} catch (error) {
		console.log('commonUpload() request fail ', error);
		throw error; // 抛出错误
	}
}
// 通过id获取大致信息
export async function getUserSomeMsg(id) {
	//
	try {
		const res = await http.get(`/user/getortherInfo`, {

			//0(待同意列表)，1（已添加）
			params: {
				id: id
			},
		})

		return res;

	} catch (error) {
		console.log('getUserSomeMsg() request fail ', error);
		throw error; // 抛出错误
	}
}

////书籍start

// 分页条件展示简略谜题信息
export async function getBookByMsg(data) {
	//data => count:int  、files：array

	try {
		const res = await http.post(`/user/book/getbookInfo`, data)

		return res;

	} catch (error) {
		console.log('getBookByMsg() request fail ', error);
		throw error; // 抛出错误
	}
}



//获取添加好友列表
export async function partnerAdd(data) {
	//data => count:int  、files：array

	try {
		const res = await http.post(`/user/partner/update`, data)

		return res;

	} catch (error) {
		console.log('partnerAdd() request fail ', error);
		throw error; // 抛出错误
	}
}

// 修改好友备注或申请状态
export async function partnerUpdate(id, status = 1, remark) {
	return await http.post(`/user/partner/update`, {
		"frdId": id,
		"remark": remark,
		"status": status,
	})
}

// 根据朋友id删除彼此
export async function partnerDelete(id) {
	return await http.delete(`/user/partner/delete?freId=${id}`)
}

// 根据谜题id展示详细谜题信息
export async function getPuzzleMoreMsg(id) {
	//
	try {
		const res = await http.get(`/user/puzzle/list/${id}`)

		return res;

	} catch (error) {
		console.log('getPuzzleMoreMsg() request fail ', error);
		throw error; // 抛出错误
	}
}

// 修改谜题信息
export async function updatePuzzle(obj) {
	//
	try {
		const res = await http.put(`/user/puzzle/update`, obj)

		return res;

	} catch (error) {
		console.log('updatePuzzle() request fail ', error);
		throw error; // 抛出错误
	}
}
////书籍end


// 获取书籍评论或评价start
export async function getBookComment(data) {
	//
	try {
		const res = await http.post(`/user/bookcomment/get`, data)

		return res;

	} catch (error) {
		console.log('getBookComment() request fail ', error);
		throw error; // 抛出错误
	}
}
// 获取书籍评论或评价end
// 新增书评或评论start
export async function addBookComment(data) {
	try {
		const res = await http.post(`/user/bookcomment/`, data)

		return res;

	} catch (error) {
		console.log('addBookComment() request fail ', error);
		throw error; // 抛出错误
	}
}


//展示更多热门谜题
export async function showMoreHotPuzzle(id) {
	//
	try {
		const res = await http.get(`/user/puzzle/show`)

		return res;

	} catch (error) {
		console.log('showMoreHotPuzzle() request fail ', error);
		throw error; // 抛出错误
	}
}


//条件查询用户书架中书籍
export async function showUserBooklist(bookCategories = undefined, isOrder = 0) {
	try {
		const res = await http.post(`/user/bookshelf/list`, {
			'bookCategories': bookCategories,
			'isOrder': isOrder,
		})

		return res;

	} catch (error) {
		console.log('showUserBooklist() request fail ', error);
		throw error; // 抛出错误
	}
}

//展示轮播图
export async function showActivity(status) {
	//
	try {
		const res = await http.get(`/user/activity/list`, {
			params: {
				status: status
			}
		})

		return res;

	} catch (error) {
		console.log('showActivity() request fail ', error);
		throw error; // 抛出错误
	}
}


//添加书籍到书架
export async function addtoUserBooklist(bookId) {
	try {
		const res = await http.post(`/user/bookshelf/add?bookId=${bookId}`)

		return res;

	} catch (error) {
		console.log('addtoUserBooklist() request fail ', error);
		throw error; // 抛出错误
	}
}

//查询是否会员
export async function ifMember() {
	//
	try {
		const res = await http.get(`/user/membership/getMsInfo`)

		return res;

	} catch (error) {
		console.log('ifMember() request fail ', error);
		throw error; // 抛出错误
	}
}


//批量将书籍从书架移除
export async function delfromUserBooklist(bookId) {
	try {
		const res = await http.delete(`/user/bookshelf/remove?bookIds=${bookId}`)

		return res;

	} catch (error) {
		console.log('delfromUserBooklist() request fail ', error);
		throw error; // 抛出错误
	}
}


// 修改谜题信息
export async function puzzleUpdate(params) {
	try {
		const res = await http.put('/user/puzzle/update', params);
		return res;
	} catch (error) {
		console.log('request fail puzzleUpdate()', error);
		throw error; // 抛出错误
	}
}


// 验证玩家答题信息
export async function checkPuzzleAnswer(data) {
	try {
		const res = await http.post(`/user/puzzle/judge`, data)

		return res;

	} catch (error) {
		console.log('checkPuzzleAnswer() request fail ', error);
		throw error; // 抛出错误
	}
}


//获取所有书籍分类
export async function getBookCategories() {
	try {
		const res = await http.get(`/user/bookcategory/getcategoryInfo`)

		return res;

	} catch (error) {
		console.log('getBookCategories() request fail ', error);
		throw error; // 抛出错误
	}
}


export async function getPuzzleByMsg(data) {

	console.log("data is-->" + JSON.stringify(data))
	try {
		const res = await http.post(`/user/puzzle/list`, {
			...data
		})


		return res;

	} catch (error) {
		console.log('getPuzzleByMsg() request fail ', error);
		throw error; // 抛出错误
	}
}


//获取所有商品分类信息
export async function getGoodsCategories() {
	try {
		const res = await http.get(`/user/product/getPcInfo`)


		return res;

	} catch (error) {
		console.log('getGoodsCategories() request fail ', error);
		throw error; // 抛出错误
	}
}


//根据商品分类和专区展示在售商品信息
export async function getGoods(filter) {
	try {
		const res = await http.post(`/user/product/getPdInfo`, filter)

		return res;

	} catch (error) {
		console.log('getGoods() request fail ', error);
		throw error; // 抛出错误
	}
}


//条件查询用户书架中书籍
export async function checkUserShelf(data) {
	try {
		const res = await http.post(`/user/bookshelf/list`, {
			...data
		})

		return res;

	} catch (error) {
		console.log('checkUserShelf() request fail ', error);
		throw error; // 抛出错误
	}
}

//查询动态及发布人基本信息列表(0全部，1朋友,2自己)
export async function getCommunityPosts(type, pageNo, pageSize, order = undefined) {
	try {
		const res = await http.post(`/user/dynamic/list`, {
			"orderItem": order,
			"pageNo": pageNo,
			"pageSize": pageSize,
			"status": type
		})
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('getCommunityPosts() request fail ', error);
		throw error; // 抛出错误
	}
}


//给动态点赞或取消点赞
export async function addLike(id) {
	try {
		const res = await http.put(`/user/dynamic/addlike?id=${id}`)
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('addLike() request fail ', error);
		throw error; // 抛出错误
	}
}


//获取动态点赞数
export async function getLikes(id) {
	try {
		const res = await http.get(`/user/dynamic/get?id=${id}`)
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('getLikes() request fail ', error);
		throw error; // 抛出错误
	}
}


//获取动态所有评论
export async function getPostComments(id) {
	try {
		const res = await http.get(`/dycomment/list?id=${id}`)
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('getPostComments() request fail ', error);
		throw error; // 抛出错误
	}
}


//新增评论
export async function addPostComment(id, text) {
	try {
		const res = await http.put(`/dycomment/add`, {
			"commentText": text,
			"dynamicId": id
		})
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('addPostComment() request fail ', error);
		throw error; // 抛出错误
	}
}


//添加动态
export async function addPost(payload) {
	try {
		const res = await http.post(`/user/dynamic/add`, payload)
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('addPost() request fail ', error);
		throw error; // 抛出错误
	}
}


//删除动态
export async function delPost(id) {
	try {
		const res = await http.delete(`/user/dynamic/remove?id=${id}`)
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('delPost() request fail ', error);
		throw error; // 抛出错误
	}
}


//修改动态
export async function updatePost(obj) {
	try {
		const res = await http.put(`/user/dynamic/update`, obj)
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('delPost() request fail ', error);
		throw error; // 抛出错误
	}
}


//单机验证玩家答题信息
export async function checkPuzzlePageAnswer(id, answer) {
	try {
		const res = await http.post(`/user/puzzle/answer`, {
			"answer": answer,
			"puzzleId": id,
		})
		//由于request已经返回  res.data.data，所以这里用res接收就可以
		return res;

	} catch (error) {
		console.log('checkPuzzlePageAnswer() request fail ', error);
		throw error; // 抛出错误
	}
}


//上传图片
export function uploadImg() {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			success: async (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				await uni.uploadFile({
					url: http.config.baseURL + '/common/upload?count=1',
					filePath: tempFilePaths[0],
					name: 'files',
					success: (uploadFileRes) => {
						var img = JSON.parse(uploadFileRes.data).data[0]
						console.log(img);
						resolve(img)
					}
				});
			}
		});
	})
}



//在需要发送请求的地方，引入对应的Api接口，然后调用相应的方法即可：
//import { postMenu, getMenu } from '/config/api.js';
//eg:   { custom: { auth: true }}/判断是否需要携带token等认证信息
// postMenu({ custom: { auth: true }})
//     .then((res) => {
//         // 处理成功返回的数据
//     })
//     .catch((err) => {
//         // 处理请求失败
//     });








// // post请求，获取菜单
// export const postMenu = (params, config = {}) => http.post('/ebapi/public_api/index', params, config)

// // get请求，获取菜单，注意：get请求的配置等，都在第二个参数中，详见前面解释
// export const getMenu = (data) => http.get('/ebapi/public_api/index', data)



// //登录
// export const postLogin = (params,config={})=> http.put('/user/wxlogin',params,config)


// //put
// //谜题单个添加
// export const puzzleAdd = (params,config={})=> http.post('/user/puzzle',params,config)
//   //private String puzzleName;//谜题名称
// 	// private String puzzleAnswer;//谜题答案
// 	//private int star;//谜题难度，1~5星
// 	//  private int puzzleType;//谜题类型，如0填空,1选择
// 	//Integer puzzleKinds;谜题分类
// 	// private List<PuzzleLabelDTO> labelList;标签列表
// 	//private PuzzleContentDTO puzzleContentDTO;谜题内容（图片，文本）



// //谜题更新
// //put
// export const puzzleUpdate = (params,config={})=> http.post('/user/puzzle/update',params,config)
// //


// //批量删除谜题
// //delete
// export const puzzleDeleteSome = (params,config={})=> http.post('/user/puzzle',params,config)
// //List<Long> ids

// //根据玩家id回显玩家所出简略谜题信息
// //put
// export const puzzleUpdateByid = (params,config={})=> http.post('/user/puzzle/update/{id}',params,config)


// //根据谜题id展示详细谜题信息
// //get
// export const puzzleListByid = (params,config={})=> http.post('/user/puzzle/list/{id}',params,config)
// //id






//config/api.js代码的使用方法
// await等待，注意与async结合使用
// await postMenu({ custom: { auth: true }})

// // 假设不需要在响应拦截器中自动弹出的toast，以及不想写catch(如果promise中进行reject，但是却没有catch的话会报错)
// postMenu({ custom: { auth: true, toast: false, catch: false }}).then(() => {

// })

// // get请求
// getMenu({ custom: { auth: true }}).then(() => {

// }).catch(() =>{

// })

// // 也可以直接通过uni.$u.post发出请求，注意此处需要写上接口地址
// uni.$u.http.post('/common/menu', { custom: { auth: true }}).then(() => {

// }).catch(() =>{

// })